package com.ambitor.ipro.app.sche;

import java.util.ArrayList;
import java.util.List;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.IData;
import com.ambitor.ipro.app.sche.builder.IModuleBuilder;
import com.ambitor.ipro.app.sche.builder.ModuleBuilderFactory;
import com.ambitor.ipro.exceptions.ProException;
import com.ambitor.ipro.service.TopService;
import com.ambitor.ipro.util.Constants;
import com.ambitor.ipro.util.ResultCode;
import com.taobao.api.domain.Item;

public class InstallerHelper {

	private TopService topService = null;

	private SuperDao superDao = null;

	private IData userSession = null;

	private IData userPro = null;

	private IData userStyle = null;

	public InstallerHelper(SuperDao superDao, TopService topService,
			IData userSession, IData userPro, IData userStyle) {
		this.superDao = superDao;
		this.topService = topService;
		this.userSession = userSession;
		this.userStyle = userStyle;
		this.userPro = userPro;
	}

	public List<Item> buildAllItemsToBePromoted(List<Item> staticItemList,
			List<Item> intellItemList) {
		int intellNum = userPro.getInt("INTELL_NUM");

		List<Item> filtedIntellItemList = new ArrayList<Item>();
		int i = 0;
		for (Item intellItem : intellItemList) {
			if (i == intellNum)
				break;
			boolean isExists = false;
			for (Item staticItem : staticItemList) {
				if (intellItem.getNumIid().equals(staticItem.getNumIid())) {
					isExists = true;
					break;
				}
			}
			if (isExists == false) {
				filtedIntellItemList.add(intellItem);
				i++;
			}
		}

		List<Item> allItemListToBePromoted = new ArrayList<Item>();
		String intellFirst = userStyle.getString("INTELL_FIRST_FLAG",
				Constants.FLAG_YES);

		if (Constants.FLAG_YES.equals(intellFirst)) {
			allItemListToBePromoted.addAll(filtedIntellItemList);
			allItemListToBePromoted.addAll(staticItemList);
		} else {
			allItemListToBePromoted.addAll(filtedIntellItemList);
			allItemListToBePromoted.addAll(staticItemList);
		}

		return allItemListToBePromoted;
	}

	@SuppressWarnings("unchecked")
	public List<String> buildPromHTML(List<Item> allItemListToBePromoted,
			String[] installPlaces) throws ProException {
		String sStyleId = userStyle.getString("STANDARD_STYLE");
		IData param = new DataMap();
		param.put("SSTYLE_ID", sStyleId);
		IData sstyleMap = null;
		try {
			sstyleMap = superDao.queryByPK("TD_P_SSTYLE", param);
		} catch (Exception e) {
			throw new ProException(ResultCode.DB_EXCEPTION, "查询TD_P_SSTYLE出错",
					e);
		}
		int moduleId = sstyleMap.getInt("MODULE_ID");
		IModuleBuilder moduleParser = ModuleBuilderFactory.instance(moduleId);
		moduleParser.setSuperDao(superDao);
		moduleParser.setUserPro(userPro);
		moduleParser.setUserSession(userSession);
		moduleParser.setUserStyle(userStyle);
		moduleParser.setSstyle(sstyleMap);
		moduleParser.setModuleId(moduleId);
		moduleParser.init();
		String proModule = moduleParser.buildPromHTML(allItemListToBePromoted);

		// 替换table_id，预览的时候没有userPro信息
		List<String> proModules = new ArrayList<String>();
		for (String installPlace : installPlaces) {
			String newProModule = proModule.replace("@{ipromotion_id}", "ipro_"
					+ installPlace);
			proModules.add(newProModule);
		}
		return proModules;
	}

	/**
	 * 一次装多个模块到一个宝贝上
	 * 
	 * @param promotionModules
	 * @throws ProException
	 */
	public void installInTaobao(String installIid, List<String> promotionModules)
			throws ProException {
		String nick = userSession.getString("NICK");
		String sessionId = userSession.getString("SESSION_ID");

		Long installNumIid = Long.valueOf(installIid);
		topService.intallPromotionModule(sessionId, nick, installNumIid,
				promotionModules);
	}
}
